import { Scene } from '@babylonjs/core/scene';
import { AbstractControl, IAbstractControlOptions } from './abstract-control';
import { Rectangle } from '@babylonjs/gui/2D/controls/rectangle';
import { CreateHandleTo, IHandle, RemoveHandle } from '../../data/data-handle';
import { Control } from '@babylonjs/gui/2D/controls/control';

/**
 * 矩形控件参数对象
 * @param background 背景颜色
 * @param cornerRadius 圆角半径
 * @param borderThickness 边框厚度
 * @param color 颜色
 */
interface IUIRectangleOptions extends IAbstractControlOptions {
    background?: string,
    cornerRadius?: number,
    borderThickness?: number,
    color?: string,
}


/**
 * 矩形区域控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 */
class UIRectangle extends AbstractControl {

    private _enterHandles: IHandle<void>[] = [];
    private _outHandles: IHandle<void>[] = [];
    private _downHandles: IHandle<void>[] = [];
    private _upHandles: IHandle<void>[] = [];

    constructor(name: string, scene: Scene, options?: IUIRectangleOptions) {
        super(name, scene, options);
    }

    /**
     * 设置背景颜色
     */
    public set background(color: string) {
        (this.control as Rectangle).background = color;
    }

    /**
     * 获取背景颜色
     */
    public get background(): string {
        return (this.control as Rectangle).background;
    }

    /**
     * 添加进入回调函数对象
     * @param func 回调函数
     * @returns 回调函数对象
     */
    public onEnter(func: { (): void }): IHandle<never> {
        return CreateHandleTo(func, this._enterHandles);
    }

    /**
     * 移除进入回调函数对象
     * @param handle 回调函数对象或其id
     */
    public removeEnterHandle(handle: number | IHandle<void>): void {
        RemoveHandle(handle, this._enterHandles);
    }

    /**
     * 添加退出回调函数对象
     * @param func 回调函数
     * @returns 回调函数对象
     */
    public onOut(func: { (): void }): IHandle<never> {
        return CreateHandleTo(func, this._outHandles);
    }

    /**
     * 移除退出回调函数对象
     * @param handle 回调函数对象或其id
     */
    public removeOutHandle(handle: number | IHandle<void>): void {
        RemoveHandle(handle, this._outHandles);
    }

    /**
     * 添加按下回调函数对象
     * @param func 回调函数
     * @returns 回调函数对象
     */
    public onDown(func: { (): void }): IHandle<never> {
        return CreateHandleTo(func, this._downHandles);
    }

    /**
     * 移除按下回调函数对象
     * @param handle 回调函数对象或其id
     */
    public removeDownHandle(handle: number | IHandle<void>): void {
        RemoveHandle(handle, this._downHandles);
    }

    /**
     * 添加抬起回调函数对象
     * @param func 回调函数
     * @returns 回调函数对象
     */
    public onUp(func: { (): void }): IHandle<never> {
        return CreateHandleTo(func, this._upHandles);
    }

    /**
     * 移除抬起回调函数对象
     * @param handle 回调函数对象或其id
     */
    public removeUpHandle(handle: number | IHandle<void>): void {
        RemoveHandle(handle, this._upHandles);
    }

    protected initializeControl(name: string, scene: Scene, options: IUIRectangleOptions): Rectangle {
        const rect = new Rectangle(name + '-control');
        rect.cornerRadius = options.cornerRadius || 0;
        rect.thickness = options.borderThickness || 0;
        rect.background = options.background || '#000000';
        rect.color = options.color || '#ffffff';
        return rect;
    }

    protected bindEvent(control: Rectangle, options: IAbstractControlOptions): void {
        control.onPointerEnterObservable.add(() => {
            if (!this.isEnabled) return;
            if (!this.avoidEventHandle) this._enterHandles.forEach(handle => handle.callback());
        });
        control.onPointerOutObservable.add(() => {
            if (!this.isEnabled) return;
            if (!this.avoidEventHandle) this._outHandles.forEach(handle => handle.callback());
        });
        control.onPointerDownObservable.add(() => {
            if (!this.isEnabled) return;
            if (!this.avoidEventHandle) this._downHandles.forEach(handle => handle.callback());
        });
        control.onPointerUpObservable.add(() => {
            if (!this.isEnabled) return;
            if (!this.avoidEventHandle) this._upHandles.forEach(handle => handle.callback());
        });
    }

    protected unbindEvent(control: Control): void {
        super.unbindEvent(control);
        control.onPointerEnterObservable.clear();
        control.onPointerOutObservable.clear();
        control.onPointerDownObservable.clear();
        control.onPointerUpObservable.clear();
    }

}


/**
 * 创建矩形区域控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 * @returns 矩形控件
 */
const CreateUIRectangle = (name: string, scene: Scene, options?: IUIRectangleOptions): UIRectangle => {
    return new UIRectangle(name, scene, options);
};


export { UIRectangle, CreateUIRectangle };
export type { IUIRectangleOptions };